package org.jeecg.modules.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.jeecg.modules.gqHealth.entity.AuthenticationResponse;

import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class CommonUtil {


    /**
     * 身份证图片base64字符串【注意：base64 或 url 必须提供一个】
     * @param sfz_base64
     * @param sfz_url
     * @return
     */
    public static AuthenticationResponse getAuthenticationOrcIdcard(String sfz_base64, String sfz_url) {
        String host = "https://jmsfzsbbd.market.alicloudapi.com";
        String path = "/ocr/idcard-enhance";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + SyssetConfConstants.AUTH_ORC_APPCODE);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("photoBase64",sfz_base64);
        bodys.put("photoUrl",sfz_url);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
            //获取response的body
            String jsonString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            JSONObject jsonObject = JSON.parseObject(jsonString);
            // 访问嵌套的对象和数组
            JSONObject dataObject = jsonObject.getJSONObject("data");
            int result = dataObject.getIntValue("result");
            JSONObject infoObject = dataObject.getJSONObject("info");
            String number = infoObject.getString("number");
            String address = infoObject.getString("address");
//          String nation = infoObject.getString("nation");
//          String sex = infoObject.getString("sex");
            String name = infoObject.getString("name");
            AuthenticationResponse authenticationResponse = new AuthenticationResponse();
            authenticationResponse.setName(name);
            authenticationResponse.setNumber(number);
            authenticationResponse.setAddress(address);
            return authenticationResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**实名
     * 返回 code
     *  "code": 400, 身份证或姓名是否正确
     *  "code": 200,成功
     *   "code": 其他失败
     */
    public static Integer getIdcardValidate(String name,String number) {
        String host = "https://jmidcardv1.market.alicloudapi.com";
        String path = "/idcard/validate";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + SyssetConfConstants.AUTH_ORC_APPCODE);
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idCardNo", number);
        bodys.put("name", name);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
            //获取response的body
            String jsonString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            JSONObject jsonObject = JSON.parseObject(jsonString);
            JSONObject dataObject = jsonObject.getJSONObject("data");
            String code = jsonObject.getString("code");
            if (!code.equals("200")){
                return 0;
            };
            String msg = jsonObject.getString("msg");
            int result = dataObject.getIntValue("result");// "result": 1, // 0: 一致，1:  不一致
            if (result == 0 ){
                return 200;
            }
//            return Integer.valueOf(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
    * 计算第二天凌晨与当前时间的时间差秒数
     * @param
     * @return java.lang.Long
     *
     * @date
     */
 public static Long getNowToNextDaySeconds() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

}
